package android.zhcs.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.zhcs.myapplication.databinding.ActivityMainBinding;
import android.zhcs.myapplication.databinding.ChatboxBinding;
import android.zhcs.myapplication.databinding.Item2Binding;
import android.zhcs.myapplication.ui.dashboard.DashboardFragment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.CompositePageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomsheet.BottomSheetDialog;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class ksMainActivity2 extends AppCompatActivity {
    private ActivityMainBinding minb;
    private int nowt;
    private ArrayList<bean1> lists;
    private ArrayList<bean1> lists2 = new ArrayList<>();
    private TextView zjg;
    double zj = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        minb = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(minb.getRoot());
        getSupportActionBar().hide();
        setContentView(R.layout.activity_ks_main2);
        ViewPager2 viewPager2 = findViewById(R.id.viewpager);
        View gwc = findViewById(R.id.gwc);
        Myrecc myrecc = findViewById(R.id.marecc);
        zjg = findViewById(R.id.zjg);
        zjg.setText("￥: 0");
        gwc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(ksMainActivity2.this);
                ChatboxBinding binding = ChatboxBinding.inflate(getLayoutInflater());
                bottomSheetDialog.setContentView(binding.getRoot());
                binding.myreccc.setAdapter(1, lists2.size(), R.layout.item2, new Caa() {
                    @Override
                    public void bix(View view, int position) {
                        bean1 a = lists2.get(position);
                        Item2Binding inflate = Item2Binding.bind(view);

                        inflate.name.setText(a.name);
                        inflate.jg.setText(a.price + "");
                        inflate.num.setText(a.num + "");
                        inflate.jia.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                            }
                        });
                    }
                });

                binding.jg.setText("￥：" + zj);
                bottomSheetDialog.show();
            }
        });
        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @NotNull
            @Override
            public Fragment createFragment(int position) {
                return new DashboardFragment(position);
            }

            @Override
            public int getItemCount() {
                return 2;
            }
        });
        viewPager2.setOffscreenPageLimit(99);
        viewPager2.setPageTransformer(new CompositePageTransformer());
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                nowt = position;
                super.onPageSelected(position);
            }
        });
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        viewPager2.setCurrentItem(nowt++);
                        if (nowt == 2) nowt = 0;
                    }
                });
            }
        };
        timer.schedule(timerTask, 0, 1000);
        lists = new ArrayList<>();
        lists.add(new bean1("鱼香肉丝01", "https://th.bing.com/th/id/OIP.XUR3h4TEtSuJKNnMCsyQ7gHaE8?w=296&h=197&c=7&r=0&o=5&pid=1.7", 102.1));
        lists.add(new bean1("鱼香肉丝02", "https://th.bing.com/th/id/OIP.XUR3h4TEtSuJKNnMCsyQ7gHaE8?w=296&h=197&c=7&r=0&o=5&pid=1.7", 202.1));
        lists.add(new bean1("鱼香肉丝03", "https://th.bing.com/th/id/OIP.XUR3h4TEtSuJKNnMCsyQ7gHaE8?w=296&h=197&c=7&r=0&o=5&pid=1.7", 302.1));
        lists.add(new bean1("鱼香肉丝04", "https://th.bing.com/th/id/OIP.XUR3h4TEtSuJKNnMCsyQ7gHaE8?w=296&h=197&c=7&r=0&o=5&pid=1.7", 402.1));
        lists.add(new bean1("鱼香肉丝05", "https://th.bing.com/th/id/OIP.XUR3h4TEtSuJKNnMCsyQ7gHaE8?w=296&h=197&c=7&r=0&o=5&pid=1.7", 502.1));
        myrecc.setAdapter(1, lists.size(), R.layout.item2, new Caa() {
            @Override
            public void bix(View view, int position) {
                bean1 b = lists.get(position);
                TextView jg = view.findViewById(R.id.jg);
                TextView num = view.findViewById(R.id.num);
                ImageView img = view.findViewById(R.id.img);
                TextView name = view.findViewById(R.id.name);
                ImageView jia = view.findViewById(R.id.jia);
                ImageView jian = view.findViewById(R.id.jian);
                jg.setText("￥" + b.price);
                num.setText(b.num + "");
                jia.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int resid = Integer.parseInt(num.getText().toString()) + 1;
                        num.setText(resid + "");
                        b.num++;
                        gbjg(b);
                        cx();

                    }

                });
                jian.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (b.num != 0) {
                            int resid = Integer.parseInt(num.getText().toString()) - 1;
                            num.setText(resid + "");
                            b.num--;
                            gbjg(b);
                            cx();
                        }
                    }
                });
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL(b.img);

                            Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
                            Log.d("123", String.valueOf(bitmap));
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    img.setImageResource(R.mipmap.qwe);
                                }
                            });
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                name.setText(b.name);
            }
        });
    }

    private void gbjg(bean1 b) {
        boolean pd = true;
        for (bean1 e : lists2) {
            if (e.name.equals(b.name)) {
                e.num = b.num;
                pd = false;
                if (e.num == 0) {
                    lists2.remove(e);
                }
            }
        }
        if (pd){
            lists2.add(b);
        }
    }

    public void cx() {
        zj = 0;
        for (int i = 0; i < lists2.size(); i++) {
            zj = lists2.get(i).price * lists2.get(i).num;
        }
        String format = new DecimalFormat("#.##").format(zj);
        zjg.setText("￥：" + format);
    }
}